﻿using DAL;
using System.Collections.ObjectModel;
using Administratation.Classes;

namespace Administratation.Models
{
    public class CountriesModel
    {
        private ObservableCollection<Country> countries;

        public ObservableCollection<Country> Countries
        {
            get
            {
                if (this.countries == null)
                {
                    this.countries = new ObservableCollection<Country>(DatabasePersister.GetCountries());
                }
                return this.countries;
            }
        }

        public void SaveCountry(Country country)
        {
            DatabasePersister.SaveCountry(country);
        }

        public void AddCountry(Country country)
        {
            DatabasePersister.AddCountry(country);
        }

        public void DeleteCountry(Country country)
        {
            DatabasePersister.DeleteCountry(country);
        }

        public void ReloadCountries()
        {
            this.Countries.Clear();
            foreach (var country in DatabasePersister.GetCountries())
            {
                countries.Add(country);
            }
        }

        private ObservableCollection<Town> towns;

        public ObservableCollection<Town> Towns
        {
            get
            {
                if (this.towns == null)
                {
                    this.towns = new ObservableCollection<Town>(DatabasePersister.GetTowns());
                }
                return this.towns;
            }
        }

        public void SetTownsByCountry(int CountryID)
        {
            this.towns = new ObservableCollection<Town>(DatabasePersister.GetTownsByCountryID(CountryID));
        }


        internal void AddTown(Town newTown)
        {
            DatabasePersister.AddTown(newTown);
        }

        internal void ReloadTowns(int countryID)
        {
            this.Towns.Clear();
            foreach (var town in DatabasePersister.GetTownsByCountryID(countryID))
            {
                towns.Add(town);
            }
        }

        internal void DeleteTown(Town town)
        {
            DatabasePersister.DeleteTown(town);
        }
    }
}
